package com.haiertvbic.tvnow.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.haiertvbic.tvnow.R;
import com.haiertvbic.tvnow.model.ProgramDetail;
import com.haiertvbic.tvnow.pop.ProgramDetailPop;
import com.haiertvbic.tvnow.util.TimeUtil;

public class ProgramExpandAdapter extends BaseExpandableListAdapter {
	protected static final String TAG = "ExpandAdapter";
	private ArrayList<ProgramDetail> programs;
	private Activity context;
	private ExpandableListView exLV;
	private View childView;

	class ViewHodler {
		LinearLayout programInfor;

		// 节目的开始时间
		TextView startTime;
		// 节目图片
		ImageView poster;
		// 节目的名字
		TextView programName;
	}

	public ProgramExpandAdapter(ArrayList<ProgramDetail> programList,
			Activity context, ExpandableListView exLV) {
		this.programs = programList;
		this.context = context;
		this.exLV = exLV;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getChildView(final int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		View gridView;
		if (convertView == null) {
			gridView = View.inflate(context, R.layout.layout_program_child,
					null);
		} else {
			gridView = convertView;
		}
		childView = gridView;
		GridView gv = (GridView) gridView.findViewById(R.id.gv);
		ArrayList<String> strs = new ArrayList<String>();
		strs.add(context.getString(R.string.detail));
		strs.add(context.getString(R.string.change_channel));
		gv.setAdapter(new ChildAdapter(strs, context));
		gv.requestFocus();
		gv.setFocusable(true);
		gv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				if (arg2 == 0) {
					// 表示察看详情
					new ProgramDetailPop(context, childView, programs
							.get(groupPosition)).showPop();
				} else if (arg2 == 1) {
					// 表示换台
					Toast.makeText(context, "换台", 1).show();
				}
			}
		});
		gv.setOnKeyListener(new OnKeyListener() {

			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				switch (keyCode) {
				case KeyEvent.KEYCODE_DPAD_UP:
				case KeyEvent.KEYCODE_DPAD_DOWN:
					v.setFocusable(false);
					exLV.collapseGroup(groupPosition);
					exLV.setSelection(groupPosition);
					Drawable drawable = context.getResources().getDrawable(
							R.color.red_color);
					exLV.setSelector(drawable);
					return true;

				default:
					break;
				}
				return false;
			}
		});

		return gridView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		if (programs.size() == (groupPosition + 1)) {
			return 0;
		} else {
			return 1;
		}
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return programs.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		ViewHodler hodler = new ViewHodler();
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.layout_program_item,
					null);
			hodler.programInfor = (LinearLayout) convertView
					.findViewById(R.id.program_infor);

			hodler.startTime = (TextView) convertView
					.findViewById(R.id.detail_stime_tv);
			hodler.poster = (ImageView) convertView
					.findViewById(R.id.detail_iv);
			hodler.programName = (TextView) convertView
					.findViewById(R.id.detail_pro_tv);
			convertView.setTag(hodler);
		} else {
			hodler = (ViewHodler) convertView.getTag();
		}
		ProgramDetail program = programs.get(groupPosition);
		String startTemp = null;
		String nameTemp = null;
		startTemp = program.getPstime() + "c";
		nameTemp = program.getPn();
		if (program.getCname() != null) {
			hodler.programInfor.setGravity(Gravity.CENTER | Gravity.LEFT);
			hodler.startTime.setVisibility(View.VISIBLE);
			hodler.programName.setGravity(Gravity.LEFT);
			if (TimeUtil.isTomorrowWeek(program)) {
				String tomorrow = context.getResources().getString(
						R.string.tomorrow);
				startTemp = tomorrow + program.getPstime() + "c";
			}
			nameTemp = "    " + nameTemp;
			startTemp = startTemp.replace(":00c", "");
			startTemp = "    " + startTemp;// 四个空格
			hodler.startTime.setText(startTemp);
		} else {
			hodler.programInfor.setGravity(Gravity.CENTER);
			hodler.startTime.setVisibility(View.GONE);
			hodler.programName.setGravity(Gravity.CENTER);
		}

		hodler.programName.setText(nameTemp);

		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

}
